package com.gy.def.utils;

import com.gy.def.enums.ResultEnum;
import com.gy.def.exception.GySchoolException;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

  /**
   * 获取当前时间前5分钟
   */
  public static String getFiveMinuteAgo() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Calendar beforeTime = Calendar.getInstance();
    beforeTime.add(Calendar.MINUTE, -5);// 5分钟之前的时间
    Date beforeD = beforeTime.getTime();
    return sdf.format(beforeD);
  }

  /**
   * 判断时间是不是今天
   *
   * @param dateStr 时间字符串
   * @return 是返回true，不是返回false
   */
  public static boolean isToday(String dateStr) {
    //当前时间
    Date now = new Date();
    SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
    //获取今天的日期
    String nowDay = sf.format(now);
    //对比的时间
    String day = sf.format(dateStr2Date(dateStr));
    return day.equals(nowDay);
  }

  /**
   * 判断时间间隔是否超过传入时间
   *
   * @param dateStr 时间字符串
   * @return 是返回true，不是返回false
   */
  public static boolean overTimeOut(String dateStr, Integer timeOutValue) {
    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-M-d HH:mm:ss");
    //当前时间
    Date now = new Date();
    //获取今天的日期
    String nowDay = sdf.format(now);
    Date start;
    Date end;
    try {
      start = sdf.parse(dateStr);
    } catch (Exception e) {
      throw new GySchoolException(ResultEnum.TIME_TRANSFORM_ERROR);
    }
    try {
      end = sdf.parse(nowDay);
    } catch (Exception e) {
      throw new GySchoolException(ResultEnum.TIME_TRANSFORM_ERROR);
    }
    long cha = end.getTime() - start.getTime();
    // 说明小于指定时间
    return cha <= timeOutValue;
  }

  /**
   * 将字符串转时间
   *
   * @param dateStr 时间字符串
   * @return date类型时间
   */
  private static Date dateStr2Date(String dateStr) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date;
    try {
      date = formatter.parse(dateStr);
    } catch (Exception ex) {
      throw new GySchoolException(ResultEnum.TIME_TRANSFORM_ERROR);
    }
    return date;
  }

  /*
   * 将时间转换为时间戳
   */
  public static String dateToStamp(String s) {
    String res;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date;
    try {
      date = simpleDateFormat.parse(s);
      long ts = date.getTime();
      res = String.valueOf(ts);
      return res;
    } catch (ParseException e) {
      e.printStackTrace();
    }
    return "";
  }

  /*
   * 将时间戳转换为时间
   */
  public static String stampToDate(String s) {
    String res;
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    long lt = new Long(s);
    Date date = new Date(lt);
    res = simpleDateFormat.format(date);
    return res;
  }

  /**
   * 判断两个日期的大小
   *
   * @param DATE1 日期1
   * @param DATE2 日期2
   */
  public static int compareDate(String DATE1, String DATE2) {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    try {
      Date dt1 = df.parse(DATE1);
      Date dt2 = df.parse(DATE2);
      if (dt1.getTime() > dt2.getTime()) {
        return -1;//dt1在dt2后
      } else if (dt1.getTime() < dt2.getTime()) {
        return 1;//dt1在dt2前
      } else {
        return 0;//dt1等dt2
      }
    } catch (Exception exception) {
      exception.printStackTrace();
    }
    return 0;
  }

  public static Integer dayForWeek(String pTime) {
    String[] weekDays = {"7", "1", "2", "3", "4", "5", "6"};
    Calendar cal = getCalendarTime(pTime);
    int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
    if (w < 0)
      w = 0;

    return Integer.valueOf(weekDays[w]);
  }

  public static Integer dayForMonth(String pTime) {

    Calendar cal = getCalendarTime(pTime);

    return cal.get(Calendar.MONTH) + 1;
  }

  private static Calendar getCalendarTime(String pTime) {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    Date tmpDate = null;
    try {
      tmpDate = format.parse(pTime);
    } catch (ParseException e) {
      e.printStackTrace();
    }
    Calendar cal = Calendar.getInstance();
    cal.setTime(tmpDate);

    return cal;
  }
}
